import React from 'react';
import { inject, observer } from 'mobx-react';
import allStore from '../../stores';
import {Condition,getAdButtons} from './ListCondition';
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import {Synergy} from 'weaPortal';
import ListLeftTree from './ListLeftTree';
import * as API_RECYCLE from '../../apis/recycle';
import {
    WeaTop,WeaTab,WeaLeftTree,WeaLeftRightLayout,WeaSearchGroup,WeaRightMenu,WeaPopoverHrm,
    WeaInput,WeaInputSearch,WeaDialog,WeaSelect,WeaLocaleProvider
} from 'ecCom'
import {WeaErrorPage,WeaTools} from 'ecCom'
import {Button,Modal,Input,Alert,notification,message} from 'antd'
import * as ListUtil from '../../util/listUtil';
const getLabel = WeaLocaleProvider.getLabel;


@inject('wfRecycleStore')

@observer
class Recycle extends React.Component {
    constructor(props){
        super(props);
    }
    doInit(props){
        const { location,wfRecycleStore } = props;
        const {appendBaseParams,initDatas,doSearch,initRecycle,doSearchRecycle} = wfRecycleStore;
        
        appendBaseParams(location.query);
        initDatas({method:"all"});
        doSearch();
        if(!wfRecycleStore.title){
            wfRecycleStore.title = getLabel(129343,'流程回收站');
        }
        _ListenerCtrl.bind();
    }
    componentDidMount() {
        this.doInit(this.props);
    }
    componentWillUnmount(){
        const { wfRecycleStore } = this.props;
        wfRecycleStore.clearStatus();
        _ListenerCtrl.unbind();
    }
    componentWillReceiveProps(nextProps) {
        if(this.props.location.key !== nextProps.location.key) {
            const { wfRecycleStore } = this.props;
            wfRecycleStore.clearStatus();
            this.doInit(nextProps);
        }
        if(window.location.pathname.indexOf('/spa/workflow/index') >= 0 && nextProps.title && document.title !== nextProps.title)
            document.title = nextProps.title;
    }
    
    render(){
        const isSingle = window.location.pathname.indexOf('/spa/workflow/index') >= 0;
        const {  wfRecycleStore} = this.props;
        const {loading,title,searchParams,showSearchAd,searchParamsAd,showBatchSubmit,setShowSearchAd,form,tableStore} = wfRecycleStore;
        const loadingTable = tableStore.loading;
        const formParams = form.getFormParams() || {};
        let buttons = this.getButtons();
        let dropMenuDatas = this.getRightMenu();
      
        return(
            <div>
                {isSingle && <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@hjj115`} />}
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@1u5iqp`} 
                    collectParams={{favname:getLabel(129343,'流程回收站'), favouritetype:2}}
                    datas={dropMenuDatas}>
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@450sjn`}
                        title={title}
                        loading={ loading || loadingTable}
                        icon={<i className='icon-coms-workflow' />}
                        iconBgcolor='#0079DE'
                        buttons={buttons}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={dropMenuDatas}
                    >
                        <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@1lx428`} 
                            leftCom={<ListLeftTree ecId={`${this && this.props && this.props.ecId || ''}_ListLeftTree@9pium4`} listStore={wfRecycleStore} />}
                            defaultShowLeft={true}  
                        >
                            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@iobl6w`}
                                buttonsAd={getAdButtons(wfRecycleStore, this.props && this.props.ecId || '')}
                                searchType={['base','advanced']}
                                searchsBaseValue={formParams.requestname}
                                setShowSearchAd={bool=>{setShowSearchAd(bool)}}
                                hideSearchAd={()=> setShowSearchAd(false)}
                                searchsAd={
                                    <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@4l0d0y`} listStore={wfRecycleStore} />
                                }
                                showSearchAd={showSearchAd}
                                /* datas={topTab} */
                                selectedKey={searchParams.viewcondition}
                                /* counts={topTabCount} */
                                onSearch={v=>{wfRecycleStore.doSearch()}}
                                onSearchChange={v=>{wfRecycleStore.appendFormFields({requestname:{value:v}})}}
                                keyParam="viewcondition"  //主键
                                countParam="groupid" //数量
                                onChange={this.switchTab.bind(this)} />
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@6nulmt`}
                                comsWeaTableStore={tableStore}
                                hasOrder={true}
                                needScroll={true}
                            />
                        </WeaLeftRightLayout>
                    </WeaTop>
                </WeaRightMenu>
                
            </div>
        )
    }

    switchTab(tabKey) {
        const {wfRecycleStore} = this.props;
        const {topTab} = wfRecycleStore;
        wfRecycleStore.setShowSearchAd(false);
        wfRecycleStore.doSearch({
            viewcondition: topTab[tabKey].viewcondition
        });
    }
    //获取右键菜单
    getRightMenu(){
        const {  wfRecycleStore } = this.props;
        const { doSearch , setShowSearchAd  , tableStore} = wfRecycleStore;
        const selectedRowKeys = tableStore.selectedRowKeys;
        const selectedLen = tableStore.selectedRowKeys.length;
        const current = tableStore.current;
    	let btns = [];
    	btns.push({
            key: "search",
    		icon: <i className='icon-workflow-Right-menu--search'/>,
            content:getLabel(197,'搜索'),
            onClick: key =>{
               doSearch();
    		   setShowSearchAd(false);
            }
        });
        btns.push({
            key: "restore",
    		icon: <i className='icon-coms-Rightmenu-Revoke'/>,
            content:getLabel(385193,'恢复'),
            disabled: selectedLen <= 0,
            onClick: key =>{
                Modal.confirm({
                    title:getLabel(131329,' 信息确认'),
                    content: getLabel(131519,'确定要恢复吗？'),
                    onOk() {
                        if(selectedLen>0){
                            message.loading(getLabel(383167,'正在恢复，请稍候...'),0);
                            API_RECYCLE.getRecycleReStore(selectedRowKeys).then((data)=>{ 
                                message.destroy();
                                message.success(getLabel(383168,'恢复成功')+'！');
                                if(data&&data.fail!=0){
                                    message.error(getLabel(130665,'恢复失败！'));
                                }
                                wfRecycleStore.initDatas();
                                wfRecycleStore.doSearch({current});
                            })
                        }
                        
                    },
                    onCancel() {},
                    okText:getLabel(83446,'确定'),
                    cancelText:getLabel(31129,'取消')
                });
            }
        });
        btns.push({
            key:"colSet",
    		icon: <i className='icon-coms-Custom'/>,
            content:getLabel(32535,'显示列定制'),
            onClick: key =>{
                tableStore.setColSetVisible(true);
                tableStore.tableColSet(true);
            }
    	});
    	return btns;
    }
    getButtons() {
        const { wfRecycleStore } = this.props;
        const { form , tableStore} = wfRecycleStore;
        const selectedRowKeys = tableStore.selectedRowKeys;
        const selectedLen =tableStore.selectedRowKeys.length;
        const current = tableStore.current;
        let btns = [];
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@cbz0f7@$back`} 
                    disabled={selectedLen <= 0}
                    type="primary" 
                    onClick={()=>{
                        Modal.confirm({
                            title:getLabel(131329,' 信息确认'),
                            content: getLabel(131519,'确定要恢复吗？'),
                            onOk() {
                                if(selectedLen>0){
                                    message.loading(getLabel(383167,'正在恢复，请稍候...'),0);
                                    API_RECYCLE.getRecycleReStore(selectedRowKeys).then((data)=>{
                                        
                                        message.destroy();
                                        message.success(getLabel(383168,'恢复成功')+'！');
                                        if(data&&data.fail!=0){
                                            message.error(getLabel(130665,'恢复失败！'));
                                        }
                                        wfRecycleStore.initDatas();
                                        wfRecycleStore.doSearch({current});
                                    })
                                }
                                
                            },
                            onCancel() {},
                            okText:getLabel(83446,'确定'),
                            cancelText:getLabel(31129,'取消')
                        });
                    }
                    }>
                    {getLabel(385193,'恢复')}
                </Button>);
        //
        return btns
    }
    }

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@ba9dsb`} msg={ props.error ? props.error : getLabel(383324,' 对不起，该页面异常，请联系管理员 ')+'！'} />,
    {error: ""}
    )(Recycle);

//全局恢复方法
window.workflowRecycleListUtil =(function(){
    
	return {
		doRestore(requestid, otherpara, obj){
            let data = {
                requestid : requestid,
                actiontype : "restore",
                viewScope : "delete"
            }
            const listStore = allStore['wfRecycleStore'];
            const {tableStore} = listStore;
            const current = tableStore.current;

            Modal.confirm({
                title:getLabel(131329,' 信息确认'),
                content: getLabel(131519,'确定要恢复吗？'),
                onOk() {
                    message.loading(getLabel(383167,'正在恢复，请稍候...'),0);
                    {
                        jQuery.ajax({
                            type : "POST",
                            url : "/api/workflow/delete/getList",
                            data:data,
                            success : (data) => {
                                message.destroy();
                                let res = JSON.parse(data);
                                if(res.success==1){
                                    message.success(getLabel(383168,'恢复成功')+'！');
                                    listStore.initDatas();
                                    listStore.doSearch({current});
                                }
                                else{
                                    message.error(getLabel(130665,'恢复失败！'));
                                }
                            },
                            error:(err)=>{
                            }
                        });
                    }
                },
                onCancel() {},
                okText:getLabel(83446,'确定'),
                cancelText:getLabel(31129,'取消')
            });
        }
	}
})()

